package com.gem.talkbase.activity;

import org.json.JSONException;
import org.json.JSONObject;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.gem.talkbase.R;

import com.gem.talkbase.client.Client;
import com.gem.talkbase.util.MyTextWatcher;
import com.gem.talkbase.util.XmppTool;

import com.gem.talkbase.util.T;



import android.annotation.TargetApi;
import android.app.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;

import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
public class ChangePassActivity extends Activity {
   

	private EditText et1;  
	private EditText et2;
	private Button  bt1;
	private String requestPath=Client.ChangePass_URL;
	SharedPreferences sharedPreferences; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.changepass);
		sharedPreferences = getSharedPreferences(Client.SAVE_USER,MODE_PRIVATE);
		et1 = (EditText) findViewById(R.id.pass_1);	
		et1.addTextChangedListener(new NewWatcher());
		et2 = (EditText) findViewById(R.id.pass_2);
		et2.addTextChangedListener(new NewWatcher());
		bt1 = (Button) findViewById(R.id.suer);
		final String username = sharedPreferences.getString("userid", ""); //这 是登录者的手机号
		String password=sharedPreferences.getString("password", "");
	    System.out.println(username+"+++++++++++++++++++++++++++++++++++++++++++++++"+password);
		bt1.setOnClickListener(new View.OnClickListener() {   
			@Override
			public void onClick(View v) {
				System.out.println("监听开始！");
	
				String  pass1 = ChangePassActivity.this.et1.getText().toString();
				String 	pass2 = ChangePassActivity.this.et2.getText().toString();
				if(pass1.equals(pass2)==true){
					 fun(username,pass1);
				}else{
			       T.showLong(ChangePassActivity.this, "两次密码输入不一致，请重新输入！");
				}
				
				System.out.println("监听结束！");	
				

			}	
		});
		
	}
	

//===================断开连接===========================
	public void notIN()
	{
		super.onBackPressed();
		XmppTool.closeConnection();
		

	}
	public void fun(String username,String pass1){
		AjaxParams params = new AjaxParams();
		params.put("username", username);
		params.put("pass1", pass1);
		FinalHttp fh = new FinalHttp();
		fh.post(requestPath, params, new AjaxCallBack<String>() {
			@Override
			public void onFailure(Throwable t, String strMsg) {
				System.out.println("修改密码==加载失败！！！");
				super.onFailure(t, strMsg);
			}

			@Override 
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				System.out.println("修改密码==正在加载！！！");
				super.onLoading(count, current);
			}
  
			@TargetApi(Build.VERSION_CODES.HONEYCOMB)
			@Override
			public void onSuccess(String jsonString) { 
				super.onSuccess(jsonString);
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(jsonString);
					final String resString = jsonObject.getString("Change"); 
	                System.out.println(resString+"======================");
	                if("true".equals(resString)){
	                startActivity(new Intent(ChangePassActivity.this, LoginActivity.class));
	                finish();
	                notIN();
	                }
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	         }
		});
	}
	
	
	
	
	
	
	
	
	
	

	//===========================监控EditText输入===================================
	public class NewWatcher extends MyTextWatcher {

		@Override //文字变化时  
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			String  pass1 = ChangePassActivity.this.et1.getText().toString();
			String 	pass2 = ChangePassActivity.this.et2.getText().toString();

			//==================如果输入框里面有值，则可以点击=======================
			if(pass2.trim().length()>0&&pass1.trim().length()>0){
				bt1.setEnabled(true);
				bt1.setBackgroundResource(R.drawable.blue_button4);
				
				//==================如果输入框里面没有值，则不可以点击=======================
			}else{
				bt1.setEnabled(false); 
				bt1.setBackgroundResource(R.drawable.blue_button);
				
			} 
		}

		@Override//文字变化前  
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			//	 System.out.println("beforeTextChanged--" + username.getText().toString()+"???????"+password.getText().toString());
		}



		@Override//文字变化后  
		public void afterTextChanged(Editable s) {
			// System.out.println("afterTextChanged--" + username.getText().toString()+"???????"+password.getText().toString());

		}

	}
}
